﻿using System;
using System.Web;
using JUtil;

/// <summary>
/// HttpModule01 的摘要描述
/// </summary>
public class HttpModuleLifeCycle : IHttpModule
{
    #region IHttpModule members

    public void Dispose()
    {
        Log.D("Dispose");
    }

    public void Init(HttpApplication context)
    {
        /// --------------------------------------------------------------------------------
        // 發生於 ASP.NET 回應要求時，做為 HTTP 管線的執行鏈結裡的第一個事件
        context.BeginRequest += new EventHandler(context_BeginRequest);
        // 發生於安全性模組建立使用者的識別 (Identity) 時
        context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
        // 發生於安全性模組建立使用者的識別 (Identity) 時
        context.PostAuthenticateRequest += new EventHandler(context_PostAuthenticateRequest);
        // 發生於安全性模式驗證使用者授權時
        context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
        // 當目前要求的使用者已獲授權時發生
        context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest);

        /// --------------------------------------------------------------------------------
        // 當 ASP.NET 略過目前事件處理常式的執行並允許快取模組從快取中服務要求時發生
        context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
        // 當 ASP.NET 略過目前事件處理常式的執行並允許快取模組從快取中服務要求時發生
        context.PostResolveRequestCache += new EventHandler(context_PostResolveRequestCache);
        // 當 ASP.NET 已對應目前要求至適當事件處理常式時發生
        context.PostMapRequestHandler += new EventHandler(context_PostMapRequestHandler);
        // 當 ASP.NET 取得與目前要求關聯的目前狀態 (例如，工作階段狀態) 時發生
        context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        // 當取得與目前要求關聯的要求狀態 (例如，工作階段狀態) 時發生
        context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);

        /// --------------------------------------------------------------------------------
        // 正好發生於 ASP.NET 開始執行事件處理常式 (例如，網頁或 XML Web Service) 之前
        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        // 當 ASP.NET 事件處理常式 (例如，網頁或 XML Web Service) 完成執行時發生
        context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
        // 當 ASP.NET 已完成執行所有要求事件處理常式並已儲存要求狀態資料時發生
        context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
        // 當 ASP.NET 已完成執行所有要求事件處理常式並已儲存要求狀態資料時發生
        context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState);
        // 當 ASP.NET 完成更新快取模組並儲存回應 (用來從快取中服務後續的要求) 時發生
        context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);

        /// --------------------------------------------------------------------------------
        // 當 ASP.NET 完成更新快取模組並儲存回應 (用來從快取中服務後續的要求) 時發生
        context.PostUpdateRequestCache += new EventHandler(context_PostUpdateRequestCache);
        // 發生於 ASP.NET 回應要求時，做為 HTTP 管線的執行鏈結裡的最後一個事件
        context.EndRequest += new EventHandler(context_EndRequest);
        // 正好發生於 ASP.NET 傳送 HTTP 標頭給用戶端時
        context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
        // 正好發生於 ASP.NET 傳送內容給用戶端時
        context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        // 發生於處置應用程式時
        context.Disposed += new EventHandler(context_Disposed);

        /// --------------------------------------------------------------------------------
        // 當擲回未處理的例外狀況時發生
        context.Error += new EventHandler(context_Error);
        
        /*
         * 這項作業需要 IIS Integrated 管線模式
         */
        //context.LogRequest += new EventHandler(context_LogRequest);
        //context.MapRequestHandler += new EventHandler(context_MapRequestHandler);
        //context.PostLogRequest += new EventHandler(context_PostLogRequest);
    }

    #endregion

    #region HttpApplication EventHandlers

    void context_UpdateRequestCache(object sender, EventArgs e)
    {
        Log.D("context_UpdateRequestCache");
    }

    void context_ResolveRequestCache(object sender, EventArgs e)
    {
        Log.D("context_ResolveRequestCache");
    }

    void context_ReleaseRequestState(object sender, EventArgs e)
    {
        Log.D("context_ReleaseRequestState");
    }

    void context_PreSendRequestHeaders(object sender, EventArgs e)
    {
        Log.D("context_PreSendRequestHeaders");
    }

    void context_PreSendRequestContent(object sender, EventArgs e)
    {
        Log.D("context_PreSendRequestContent");
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        Log.D("context_PreRequestHandlerExecute");
    }

    void context_PostUpdateRequestCache(object sender, EventArgs e)
    {
        Log.D("context_PostUpdateRequestCache");
    }

    void context_PostRequestHandlerExecute(object sender, EventArgs e)
    {
        Log.D("context_PostRequestHandlerExecute");
    }

    void context_PostReleaseRequestState(object sender, EventArgs e)
    {
        Log.D("context_PostReleaseRequestState");
    }

    void context_PostMapRequestHandler(object sender, EventArgs e)
    {
        Log.D("context_PostMapRequestHandler");
    }

    void context_PostLogRequest(object sender, EventArgs e)
    {
        Log.D("context_PostLogRequest");
    }

    void context_PostAuthenticateRequest(object sender, EventArgs e)
    {
        Log.D("context_PostAuthenticateRequest");
    }

    void context_PostAcquireRequestState(object sender, EventArgs e)
    {
        Log.D("context_PostAcquireRequestState");
    }

    void context_MapRequestHandler(object sender, EventArgs e)
    {
        Log.D("context_MapRequestHandler");
    }

    void context_LogRequest(object sender, EventArgs e)
    {
        Log.D("context_LogRequest");
    }

    void context_Error(object sender, EventArgs e)
    {
        Log.D("context_Error");
    }

    void context_EndRequest(object sender, EventArgs e)
    {
        Log.D("context_EndRequest");
    }

    void context_Disposed(object sender, EventArgs e)
    {
        Log.D("context_Disposed");
    }

    void context_AcquireRequestState(object sender, EventArgs e)
    {
        Log.D("context_AcquireRequestState");
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        Log.D("context_BeginRequest");
    }

    void context_AuthenticateRequest(object sender, EventArgs e)
    {
        Log.D("context_AuthenticateRequest");
    }

    void context_AuthorizeRequest(object sender, EventArgs e)
    {
        Log.D("context_AuthorizeRequest");
    }

    void context_PostAuthorizeRequest(object sender, EventArgs e)
    {
        Log.D("context_PostAuthorizeRequest");
    }

    void context_PostResolveRequestCache(object sender, EventArgs e)
    {
        Log.D("context_PostResolveRequestCache");
    }

    #endregion
}
